package com.hqyj.second.controller;

import com.hqyj.second.domain.Users;
import com.hqyj.second.json.R;
import com.hqyj.second.service.UsersService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("users")
@RestController //@Controller +@ResponseBody
public class UsersController {

    @Resource
    private UsersService usersService;

    /*
    @GetMapping("auth")
    public Map<String, Object> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Users loginUser = usersService.login(username, password);
            if (loginUser != null) {
                result.put("code", 200);
                result.put("msg", "登录成功！");
                result.put("data", loginUser);

            } else {
                result.put("code", 400);
                result.put("msg", "登录失败！请检查用户名或者密码是否正确");
            }
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "程序出现异常！");
            return result;
        }
    }*/

    @GetMapping("auth")
    public R login(@RequestParam("username") String username, @RequestParam("password") String password) {
        //Map<String, Object> result = new HashMap<String, Object>();
        try {
            Users loginUser = usersService.login(username, password);
            if (loginUser != null) {
                return R.success("登录成功！", loginUser);
            }
            return R.fail("登录失败，请检查用户名或者密码是否正确！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //更新了reg接口
    @PostMapping("/")
    public R reg(@RequestBody Users u) {
        try {
            if (usersService.insertUser(u)) {
                return R.success("恭喜您，注册成功！");
            }
            return R.fail("注册失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

}
